package com.sy.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.sy.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117687659";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDPJMRGbvLWK1s/zAPoArpI6VWq2ugjDliHRi68i7+lg0IwBk9xu/99Cb12OwrQWR8tQx65FZb9bCmC+aMUbz4CsZCAnapOW8j+P4s3s3xWLi7UGFdUFjaauhnG/v0Io8mmgqwR1SzUi31hZ5Kt7I4nceITP1J+FJ5z2dhVoNyJxfMj5ilTmWapvRF+EXtbzinYkfqtIiii5HJD6KwvuP9nKcSCLApnFPm2wnPB/RINI2AFeZQU11u72rtaI7OEcb1XyNmJDUla4pHOiuzvMN8eYotv0sR4vZsXohKcyjFA4SgnYLv3V5SWT3cb1l1Z8xUWw9b0OMS/LQmRS7br22oXAgMBAAECggEBALnG9sVYkjAkmGUCO2qkr5/erTdbtyBhqGJCPn5ZM5tkukLKFgwiGMRyrNpRn6Q2mfCFJxPF2jIVBLHz5B/amRnfWIMRZIF8biogoWJmdhfwuYz8+61tcrGYQLApVpSeGKdewKYJs3g4Vk26j2GvK9sSlzi97OUo5uduNEivybtBD3DQaoQlUoPQv9oEV2HkLzcndkLyCSKavnuymtZw4TXB8/jVLvR55AtCCKYsnenonhWeD/AvyQahHiolAWjxtrDNfA6FFzvzs3DXveXp/sNWvibYrPeZGK8wlio1S3pARVJDhpvozvQpFftw0bkhXS1StZTAIXf8JBrOex1XJJECgYEA/mDabYn63ivPa8miD3VP5WAyhnu4cyQxRHu4mlk34shd/tfeybtwZ6pYypI1Ww/qT34PuFOiGMQNw8QpY8A7kPRQbFwVcafzOP4GqiJSq6nblUhs2QAqqJOs4TTiKGnD/IdQ7w76V3/9GeE47ffpbdPYlhgBaC6+IvGUqK4d/c8CgYEA0HbTf7t0wLuxFPhU2OB/Z11Ts3nSJvXPDaVFEKB8KCV5bwGBfQeb1wVKjx5PLW7l0IrGeF3iIfTMk3dRre1Sdq52MnSdVCsvSZpZ/hKlo2N/4ttRZdPLT1Zj0vjWD5xSJcYHX4lD/rwaVcmeQNGxipdDPPXIso2hR6i5Uz2saTkCgYEAzpER7y/EA1RqPKphKtJlzetfPCwNrZqvU2BwmQ5oPoXdJoxgA0ttzRDcx6sRHuB+soy48UMg96GFV/rcbX92H5D2GfhxBXmQHjDScqgqn/HwOXq1wUXMXlaFZeAO4yh4GYI869Qq4JEzlCd7lmriKOiGEeRuiA6lIjUxNgUmn6sCgYB6Fa0FeFYG7aTmgAzIuqlCcsCrMmPl1z08Y5bQBvzPUisM3JfmDkexJHMm3/xEwlaqzCsxoOHnOwN4NX/LWVpbEQ89u1fMTEI0W8XqaydASexCgmjoNqIxw4lQq9RJbiYZ4yC8TZpZm/XcWehpU6UjYjTJhbPVBeb3HZPzcQIA2QKBgDefSr+8ytqR+noZXfGkHapuWXZopZkWHOOb73xdfNDTzuCByCwrQKggHj92VBZh4izpv4MtwKnzJiY+ra0SBYSotDAjAlXgbqTkFUwvUbRMqGrjYeEEqMM7AfoKQJvLy6Dqy4QDOxRnRgF856mMBdATF0hrb/YR1iSQ50WPFvEp";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArKbko5cnlYPcTACifKi3M3Twd7dO8Ej7bEXj8z5LRWfev+pQor1/4ZkFIc59lhQvrQOsmdVlc0G9KAmRXtd2kE/pBlSVfYSW+khFbvI5Pwte3LHnartKCbWtkorM8HGZC56ssnS3VFqcU3k3ycZvUQe/a9cZTfsQVmhVP1VZ3nJpr3J9Hva8+i2MxXwaDO1Qk5w6eEH6VXqT3xd4a4UYyS99q113/uLiy42vjjmHCzooQmzeAXpSems5L6oIPbPVtfyAmdWAa/9wwP9gjVFf99iXUqCRnLMutf3zVf6Uxy+2ht630KUb6cOjozEflVikNE0DxojBMTy+Q2CsvMwViQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://j32e7vw0lg.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.mall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"30m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
